#!/usr/bin/python3
# coding=utf-8

"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import menu
import random
if sys.version_info[0] == 2:
    from Tkinter import *
else:
    from tkinter import *

class FoodFrame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.delete_button = Button(self, text = "delete", command=self.food_delete)
        self.delete_button.pack(side = LEFT)
        self.change_button = Button(self, text = "change", command=self.food_change)
        self.change_button.pack(side = LEFT)
        self.foodprice_label = Label(self, text = "0",  width = 5)
        self.foodprice_label.pack(side = LEFT)
        self.foodname_label = Label(self, font = ("", 9))
        self.foodname_label.pack(side = LEFT)
        self.food_change()
    def get_price(self): return int(self.foodprice_label['text'])
    def food_delete(self):
        self.parent.set_totalprice(self.parent.get_totalprice() - self.get_price())
        self.parent.decrease_dish()
        self.destroy()

    def food_change(self):
        totalprice = self.parent.get_totalprice() - self.get_price()
        food = random.choice(menu.foods)
        self.foodname_label.configure(text = food[0])
        self.foodprice_label.configure(text = str(food[1]))
        totalprice += self.get_price()
        self.parent.set_totalprice( totalprice )
        
class MyApp(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.pack()
        self.button_frame = Frame(self)
        self.button_frame.pack(anchor = W)
        self.add_button = Button(self.button_frame, text = "Add", command=self.food_add)
        self.add_button.pack(side=LEFT)
        self.exit_button = Button(self.button_frame, text = "Exit", command=parent.destroy)
        self.exit_button.pack(side=LEFT)
        Label(self.button_frame, text = "Dishes:").pack(side=LEFT)
        self.totaldishes_label = Label(self.button_frame, text = "0")
        self.totaldishes_label.pack(side=LEFT)
        Label(self.button_frame, text = "Price:").pack(side=LEFT)
        self.totalprice_label = Label(self.button_frame, text = "0")
        self.totalprice_label.pack(side=LEFT)
        for i in range(5):self.food_add()
        
    def food_add(self):
        food_frame = FoodFrame(self)
        food_frame.pack(anchor = W)
        self.increase_dish()

    def set_totalprice(self, price): self.totalprice_label['text'] = str(price) 
    def get_totalprice(self): return int(self.totalprice_label['text'])
    def increase_dish(self): self.totaldishes_label['text'] = str(int(self.totaldishes_label['text'])+1)
    def decrease_dish(self): self.totaldishes_label['text'] = str(int(self.totaldishes_label['text'])-1)
    
if __name__ == "__main__":
    root = Tk()
    root.wm_title("food order")
    app = MyApp(root)
    root.mainloop()

